﻿using SCG.General;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TTEngine
{
    public class SnakeMaze : Room
    {
        //constructor
        public SnakeMaze() 
            : base()
        {
            UseConnectorPoints = true;
            EndConnector = 6;
            Matrix = new CharMatrix(13, 13);
            Matrix.Fill(Globals.TERRAIN_WALL);
            int cx = 6;
            int cy = 0;
            SCGDirection dir = SCGDirection.Down;
            int span = Globals.RandomIntFromTo(1, 3);
            bool reachedTop = false;
            do
            {
                while (span > 0)
                {
                    Matrix.Set(cx, cy, Globals.TERRAIN_FLOOR);
                    if (cy == 12)
                    {
                        reachedTop = true;
                        break;
                    }
                    span--;
                    dir.Advance(ref cx, ref cy);
                    if (cx < 0)
                        cx = 0;
                    if (cx >= 12)
                        cx = 12;
                    if (cy < 0)
                        cy = 0;
                    if (cy >= 12)
                        cy = 12;
                }

                //create a new span, make it more likely to turn toward the top
                span = Globals.RandomIntFromTo(1, 3);
                switch (dir)
                {
                    case SCGDirection.Up:
                    case SCGDirection.Down:
                        dir = Globals.Random.NextDouble() > 0.5 ? SCGDirection.Left : SCGDirection.Right;
                        break;
                    case SCGDirection.Right:
                    case SCGDirection.Left:
                        dir = Globals.Random.NextDouble() > 0.9 ? SCGDirection.Up : SCGDirection.Down;
                        break;
                }
            }
            while (!reachedTop);
            StartConnector = cx;
        }

        //clone
        public override Room Clone()
        {
            return new SnakeMaze();
        }

        public override bool IsCorridor
        {
            get
            {
                return true;
            }
        }
    }
}
